import { defineStore } from 'pinia';

// 定义状态的类型 一般情况下Pinia会自动推断出state的类型，你就无需定义接口。如果state的数据类型比较复杂，推荐你定义一个接口来描述state的类型
interface IUserState {
    token: string
    id: string
    username: string
    name: string
    avatar: string
    remark: string
    permissions: string[]
    is_system: boolean
}

//@ts-ignore
const useUserStore = defineStore('user', {
    state: (): IUserState => ({
        token: '',
        id: '',
        username: '',
        name: '',
        avatar: '',
        remark: '',
        permissions: [],
        is_system: false,
    }),
    getters: {
        isLogin(): boolean {
            return this.id != '' && this.token != '';
        },
        getToken(): string{
            return this.token
        },
    },
    actions: {
        login(data: any): void {
            this.id = data?.id;
            this.username = data?.username;
            this.name = data?.name;
            this.avatar = data?.avatar;
            this.remark = data?.remark;
            this.permissions = data?.permissions;
            this.is_system = data?.is_system;
        },
        logout(): void {
            this.token = '';
            this.id = '';
            this.username = '';
            this.name = '';
            this.avatar = '';
            this.remark = '';
            this.permissions = [];
            this.is_system = false;
        },
        setToken(token: string): void{
            this.token = token;
        },
    },
    persist: {
        enabled: true,
    },
});

export default useUserStore;
